package com.ptszyxx.popose.module.main.message.vm;

import android.app.Application;

import androidx.annotation.NonNull;

import com.ptszyxx.popose.common.enums.ActionEnum;
import com.ptszyxx.popose.common.enums.MessageNoticeEnum;
import com.ptszyxx.popose.common.utils.YActionEventUtil;
import com.ptszyxx.popose.common.utils.YActivityUtil;
import com.ysg.binding.command.BindingCommand;
import com.ysg.http.BaseResponse;
import com.ysg.http.HttpUtils;
import com.ysg.http.callback.OnSuccessResult;
import com.ysg.http.data.CommonRepository;
import com.ptszyxx.popose.common.event.ChatMessageEvent;
import com.ysg.base.BaseViewModel;
import com.ysg.bus.RxBus;
import com.ysg.bus.RxSubscriptions;
import com.ysg.bus.event.SingleLiveEvent;
import com.ysg.http.data.entity.notice.NoticeLastResult;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;


public class MessageVM extends BaseViewModel<CommonRepository> {

    public MessageVM(@NonNull Application application, CommonRepository model) {
        super(application, model);
    }

    @Override
    public void requestUploadAction() {
        YActionEventUtil.getInstance().upload(this, model, ActionEnum.message);
    }

    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        public SingleLiveEvent onRefreshEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onNoticeEvent = new SingleLiveEvent<>();
    }

    public BindingCommand onSystemMessageCommand = new BindingCommand(()-> YActivityUtil.getInstance().jumpMessageNotice(this, MessageNoticeEnum.system));
    public BindingCommand onPlatformMessageCommand = new BindingCommand(()-> YActivityUtil.getInstance().jumpMessageNotice(this, MessageNoticeEnum.platform));

    public Disposable disposable;
    @Override
    public void registerRxBus() {
        super.registerRxBus();
        disposable = RxBus.getDefault().toObservable(ChatMessageEvent.class)
                .subscribe(new Consumer<ChatMessageEvent>() {
                    @Override
                    public void accept(ChatMessageEvent event){
                        if (event != null){
                            uc.onRefreshEvent.call();
                        }
                    }
                });
    }

    @Override
    public void removeRxBus() {
        super.removeRxBus();
        RxSubscriptions.remove(disposable);
    }

    public void requestNoticeLast(){
        Map<String, String> map = new HashMap<>();
        HttpUtils.getInstance()
                .data(model.messageLast(map), this,
                        new OnSuccessResult<NoticeLastResult>() {
                            @Override
                            public void onSuccessResult(BaseResponse<NoticeLastResult> response) {
                                uc.onNoticeEvent.setValue(response.getData());
                            }
                        });
    }





}
